﻿/// <reference path="../GeneratedArtifacts/viewModel.js" />

myapp.AddEditFavorite.Delete_execute = function (screen) {
    // Write code here.
    screen.Favorite.deleteEntity();
};
myapp.AddEditFavorite.created = function (screen) {
    //default values
    screen.PlaceIsFavorite = false;

    $.ajax({
        type: 'post',
        data: {},
        url: '../web/GetUserName.ashx',
        success: function success(result) {
            screen.UserName = result; //cache user name for filtering
        }
    });
    screen.CarsFiltered.load().then(function () {
        // After data is loaded, set a default type
        if (screen.Favorite.Car == null) {
            screen.Favorite.Car = msls.iterate(screen.CarsFiltered.data).first();
        }
    });
};
myapp.AddEditFavorite.ShowFindPlace_execute = function (screen) {
    // Write code here.
    myapp.showFindPlace({
        beforeShown: function (findPlaceScreen) {
            //var newCustomer = new myapp.Customer();
            findPlaceScreen.PlaceId = (screen.Favorite.Place != null ? screen.Favorite.Place.Id : null);
            findPlaceScreen.CarId = (screen.Favorite.Car != null ? screen.Favorite.Car.Id : null);
            findPlaceScreen.PlaceIsShared = null;
            findPlaceScreen.PlaceLocation = null;
        },
        afterClosed: function (findPlaceScreen, navigationAction) {
            // If the user commits the change,
            // show the new Customer in View Screen.
            //if (navigationAction === msls.NavigateBackAction.commit) {
            //screen.Movement.Place = findPlaceScreen.PlaceId;
            var foundPlace = msls.iterate(screen.PlacesFiltered.data).where(function (item) {
                return item.Place.Id === findPlaceScreen.PlaceId;
            }).first();
            if (foundPlace) {
                screen.Favorite.Place === foundPlace;
            }
            //myapp.showViewCustomer(newCustomer);
            //}
        }
    });
};
myapp.AddEditFavorite.Details_postRender = function (element, contentItem) {
    contentItem.dataBind("screen.Favorite.Id", function (newValue) {
        contentItem.screen.details.displayName = "Favorite (" + (newValue == null ? "New" : newValue) + ")";
    });
};